package com.cgly.medical.service.impl;

import com.cgly.medical.domain.China;
import com.cgly.medical.mapper.ChinaMapper;
import com.cgly.medical.model.ChinaModel;
import com.cgly.medical.model.CityModel;
import com.cgly.medical.service.ChinaService;
import com.cgly.medical.service.CityService;
import com.cgly.medical.util.Msg;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.List;

@Service
public class ChinaServiceImpl implements ChinaService {

    @Resource
    ChinaMapper chinaMapper;

    @Resource
    CityService cityService;


    //根据id查询城市信息
    @Override
    public China selectById(Integer id) {
        China china = chinaMapper.selectById(id);
        return china;
    }

    //根据条件查询城市信息
    @Override
    public Msg getSomeChina(China china) {
        //最后的返回list
        List<CityModel> listCity = new ArrayList<>();
        //先根据条件查询到的China
        List<China> listChina1 = chinaMapper.selectSomeChina(china.getId(), china.getName());
        PageHelper.startPage(china.getPn(), china.getSize());
        List<China> listChina = chinaMapper.selectSomeChina(china.getId(), china.getName());

        if (listChina.size() == 0) {
            return Msg.fail().mess("未找到对应的城市信息");
        } else {
            for (China tempChina : listChina) {

                //临时变量
                CityModel temp = new CityModel();

                //得到cityNumber
                Integer cityNumber = tempChina.getId();

                //设置到temp里面
                temp.setCityNumber(cityNumber);

                //根据China的parentid找到它的上级城市
                China parentChina = chinaMapper.selectById(tempChina.getParentId());

                //名称设置到temp里面
                temp.setCity(tempChina.getName());

                //设置省会名称
                temp.setProvince(parentChina.getName());

                listCity.add(temp);
            }
            PageInfo<CityModel> cityModelList = new PageInfo<>(listCity, 5);
            cityModelList.setTotal(listChina1.size());
            return Msg.success().data("cityList", cityModelList);
        }

    }

    //根据条件查询城市信息
    @Override
    public Msg getAllChina(China china) {

        //先根据条件查询到的China
        List<China> listChina = chinaMapper.selectSomeChina(china.getId(), china.getName());

        if (listChina.size() == 0) {
            return Msg.fail().mess("未找到对应的城市信息");
        } else {
            return Msg.success().data("chinaList", listChina);
        }

    }

    @Override
    public Msg getAllChinaByPId() {
        List<ChinaModel> chinas = chinaMapper.selectByPId(0);
        for (int i = 0; i < chinas.size(); i++) {
            int pid = chinas.get(i).getId();
//            chinas.get(i).setLable(chinas.get(i).getName());
//            chinas.get(i).setValue(chinas.get(i).getId());
            List<ChinaModel> chinaModels = chinaMapper.selectByPId(pid);

            for (int j = 0; j < chinaModels.size(); j++) {
//                chinaModels.get(j).setLable(chinaModels.get(j).getName());
//                chinaModels.get(j).setValue(chinaModels.get(j).getId());
                int pid1 = chinaModels.get(j).getId();
                List<ChinaModel> chinaModels1 = chinaMapper.selectByPId(pid1);
                for (int k = 0; k < chinaModels1.size(); k++) {
//                    chinaModels1.get(k).setLable(chinaModels1.get(k).getName());
//                    chinaModels1.get(k).setValue(chinaModels1.get(k).getId());
                }
                chinaModels.get(j).setChildren(chinaModels1);

            }
            chinas.get(i).setChildren(chinaModels);
        }

        return Msg.success().data("chinas", chinas);
    }

//    @Override
//    public int deleteChinaById(Integer id) {
//        int row = chinaMapper.deleteChinaById(id);
//        return row;
//    }
}
